package com.mobile.util;

import com.github.pagehelper.PageInfo;
import lombok.AccessLevel;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

// 自定义分页结果类
public class PageResult<T> {
    @Getter(AccessLevel.NONE)
    @Setter(AccessLevel.NONE)
    private List<T> pageDatas;// 当前页数据列表
    private long totalResult;       // 总条数
    private int currentPage;     // 当前页码
    private int showCount;    // 每页条数

    public long getTotalResult() {
        return totalResult;
    }

    public void setTotalResult(long totalResult) {
        this.totalResult = totalResult;
    }

    public int getCurrentPage() {
        return currentPage;
    }

    public void setCurrentPage(int currentPage) {
        this.currentPage = currentPage;
    }

    public int getShowCount() {
        return showCount;
    }

    public void setShowCount(int showCount) {
        this.showCount = showCount;
    }

    public int getTotalPage() {
        return totalPage;
    }

    public void setTotalPage(int totalPage) {
        this.totalPage = totalPage;
    }

    public Optional<PageData> getExtraParams() {
        return extraParams;
    }

    public void setExtraParams(Optional<PageData> extraParams) {
        this.extraParams = extraParams;
    }

    private int totalPage;       // 总页数
//    private PageData extraParams; // 扩展参数
    @Getter(AccessLevel.NONE)
    @Setter(AccessLevel.NONE)
    private Optional<PageData> extraParams = Optional.empty();


    // 构造方法
    public PageResult(PageInfo<T> pageInfo) {
        this.pageDatas = pageInfo.getList();
        this.totalResult = pageInfo.getTotal();
        this.currentPage = pageInfo.getPageNum();
        this.showCount = pageInfo.getPageSize();
        this.totalPage = pageInfo.getPages();
    }
    // 构造方法
    public PageResult(List<T> pageDataList) {
        PageInfo<T> pageInfo = new PageInfo<>(pageDataList);
        this.pageDatas = pageInfo.getList();
        this.totalResult = pageInfo.getTotal();
        this.currentPage = pageInfo.getPageNum();
        this.showCount = pageInfo.getPageSize();
        this.totalPage = pageInfo.getPages();
    }
    // 链式添加参数
    public PageResult<T> set(String key, Object value) {
        Objects.requireNonNull(key, "Key cannot be null");
        if (!extraParams.isPresent()) {
            extraParams = Optional.of(new PageData());
        }
        extraParams.get().put(key, value);
        return this;
    }

    // 安全获取参数
    public Optional<Object> get(String key) {
        Objects.requireNonNull(key, "Key cannot be null");
        return extraParams.flatMap(map -> Optional.ofNullable(map.get(key)));
    }
    // 手动实现的 getter - 返回不可变列表
    public List<T> getPageDatas() {
        return pageDatas != null ? Collections.unmodifiableList(pageDatas) : Collections.emptyList();
    }

    // 手动实现的 setter - 防御性复制
    public void setPageDatas(List<T> pageDatas) {
        this.pageDatas = pageDatas != null
                ? new ArrayList<>(pageDatas) // 复制原始列表，防止外部修改
                : Collections.emptyList();   // 避免 null 值
    }
}
